package com.sprucetec.live.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sprucetec.live.common.dto.PageDTO;
import com.sprucetec.live.common.dto.PagedListDTO;
import com.sprucetec.live.common.dto.business.LiveChatDto;
import com.sprucetec.live.common.dto.business.LiveUserDto;
import com.sprucetec.live.common.exceptioin.SprucetecException;
import com.sprucetec.live.common.webcontext.LoginUserContext;
import com.sprucetec.live.constants.RedisKeyConstant;
import com.sprucetec.live.entity.LiveChat;
import com.sprucetec.live.entity.LiveUser;
import com.sprucetec.live.mapper.LiveUserMapper;
import com.sprucetec.live.service.LiveUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName LiveUserServiceImpl
 * @Description:
 * @Author Kenny
 * @Date 2020/3/30
 **/

@Service("liveUserService")
@Slf4j
public class LiveUserServiceImpl implements LiveUserService {

    @Autowired
    private LiveUserMapper liveUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public PagedListDTO<LiveUserDto> getLiveUserPage(LiveUserDto liveUserDto) {

        PagedListDTO<LiveUserDto> pagedListDTO = new PagedListDTO<>();
        PageHelper.startPage(liveUserDto.getPageIndex(), liveUserDto.getPageSize());

        Example example = new Example(LiveUser.class);
        if (StringUtils.isNotBlank(liveUserDto.getUserName())) {
            example.createCriteria().andLike("userName", "%" + liveUserDto.getUserName() + "%");
        }
        if (StringUtils.isNotBlank(liveUserDto.getUserCode())) {
            example.createCriteria().andLike("userCode", "%" + liveUserDto.getUserCode() + "%");
        }
        List<LiveUser> liveUsers = liveUserMapper.selectByExample(example);
        PageInfo<LiveUser> pages = new PageInfo(liveUsers);
        List<LiveUserDto> liveChatDtos = pages.getList().stream().map(x -> {
            LiveUserDto lD = new LiveUserDto();
            BeanUtils.copyProperties(x, lD);
            return lD;
        }).collect(Collectors.toList());
        pagedListDTO.setTotal((int) pages.getTotal());
        pagedListDTO.setRecords(liveChatDtos);
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageSize(liveUserDto.getPageSize());
        pageDTO.setPageIndex(liveUserDto.getPageIndex());
        pagedListDTO.setPage(pageDTO);
        return pagedListDTO;
    }

    @Transactional(rollbackFor = SprucetecException.class)
    @Override
    public int updateLiveUser(LiveUser liveUser) {
        liveUser.setUpdateTime(new Date());
        liveUser.setUpdateUserCode(LoginUserContext.getUser().getUid());
        liveUser.setUpdateUserName(LoginUserContext.getUser().getName());
        Example example = new Example(LiveUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (liveUser.getId() != null) {
            criteria.andEqualTo("id", liveUser.getId());
        }
        if (StringUtils.isNotBlank(liveUser.getUserCode())) {
            criteria.andEqualTo("userCode", liveUser.getUserCode());
        }
        int updateCount = liveUserMapper.updateByExampleSelective(liveUser, example);
        if(liveUser.getMuted() == 1){
            redisTemplate.boundHashOps(RedisKeyConstant.LiveUserMuted.liveMuted).
                    putIfAbsent(RedisKeyConstant.LiveUserMuted.liveMuted + ":" + liveUser.getUserCode(), 1);
        }else{
            redisTemplate.boundHashOps(RedisKeyConstant.LiveUserMuted.liveMuted).
                    delete(RedisKeyConstant.LiveUserMuted.liveMuted + ":" + liveUser.getUserCode());
        }
        return updateCount;
    }

}
